//
//  BaseViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "BaseViewController.h"
#import "UILabel+HWDUILabel.h"
#import "EditCell.h"
#import "SelfTextView.h"
#import "RWBChouseView.h"

@interface BaseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@property (nonatomic,strong)NSArray *titleArr;//标题
@property (nonatomic,strong)NSArray *BaseTitleArr;//标题



@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"基本信息"];
    [self initRightBarButtonWithTitle:@"保存" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    _BaseTitleArr = @[@"姓        名",@"性        别",@"最高学历",@"工作年限",@"所在城市",@"联系电话",@"联系邮箱"];
    [self.view addSubview:self.tableV];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}

#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _BaseTitleArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 200*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell"];
    if (!cell) {
        cell = [[EditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditCell"];
    }
    
    NSString *text = @"";
    if (indexPath.row == 0) {
        text = _data[@"name"];
    }else if (indexPath.row == 1){
        text = [self getSex];
    }else if (indexPath.row == 2){
        text = [self getEducation];
    }else if (indexPath.row == 3){
        text = [self getWorkingLife];
    }else if (indexPath.row == 4){
        text = _data[@"city"];
    }else if (indexPath.row == 5){
        text = _data[@"contactTelephone"];
    }else if (indexPath.row == 6){
        text = _data[@"contactMailbox"];
    }
    
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3) {
        [cell loadCell:_BaseTitleArr[indexPath.row] text:text placeHodel:[NSString stringWithFormat:@"请选择%@",_BaseTitleArr[indexPath.row]]];
        cell.textF.userInteractionEnabled = NO;
        cell.JianTouView.hidden = NO;
    }else{
        [cell loadCell:_BaseTitleArr[indexPath.row] text:text placeHodel:[NSString stringWithFormat:@"请输入%@",_BaseTitleArr[indexPath.row]]];
    }
    cell.blcok = ^(NSString *text){
        if (indexPath.row == 0) {
            [weakSelf.data setObject:text forKey:@"name"];
        }else if (indexPath.row == 4){
            [weakSelf.data setObject:text forKey:@"city"];
        }else if (indexPath.row == 5){
            [weakSelf.data setObject:text forKey:@"contactTelephone"];
        }else if (indexPath.row == 6){
            [weakSelf.data setObject:text forKey:@"contactMailbox"];
        }
    };
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    __weak typeof(self) weakSelf = self;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 190*screenW_6)];
    view.backgroundColor = [UIColor whiteColor];
    //标题
    UILabel *titleLab = [TomamaUI createLabel:CGRectMake(25*screenW_6,15*screenW_6,SCREENW-150*screenW_6,25*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:16*screenW_6] textColor:[UIColor colorWithHexString:@"#333333"] text:@"一句话介绍"];
    [view addSubview:titleLab];
    //输入框
    SelfTextView *inputTV = [[SelfTextView alloc]initWithFrame:CGRectMake(20*screenW_6, 50*screenW_6, SCREENW-40*screenW_6, 120*screenW_6) text:_data[@"introduce"] PlaceHodel:@"请输入详细介绍"];
    inputTV.blcok = ^(NSString *text){
        [weakSelf.data setObject:text forKey:@"introduce"];
    };
    inputTV.backgroundColor = backColor;
    inputTV.TextV.backgroundColor = backColor;
    [view addSubview:inputTV];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3) {
        NSArray *arr;
        if (indexPath.row == 1) {
            arr = @[@"男",@"女"];
        }else if (indexPath.row == 2){
            arr = @[@"大专",@"本科",@"硕士",@"博士",@"其他"];
        }else if (indexPath.row == 3){
            arr = @[@"应届毕业生",@"1年",@"2年",@"3年",@"4年",@"5年",@"6年",@"7年",@"8年",@"9年",@"10年",@"10年以上"];
        }
        RWBChouseView *view = [[RWBChouseView alloc]initWithTitles:arr];
        view.chouseTitleblock = ^(NSString *text){
            if (indexPath.row == 1) {
                [weakSelf.data setObject:[NSString stringWithFormat:@"%ld",[arr indexOfObject:text] + 1] forKey:@"sex"];
            }else if (indexPath.row == 2){
                [weakSelf.data setObject:[NSString stringWithFormat:@"%ld",[arr indexOfObject:text]] forKey:@"education"];
            }else if (indexPath.row == 3){
                [weakSelf.data setObject:[NSString stringWithFormat:@"%ld",[arr indexOfObject:text]] forKey:@"workingLife"];
            }
            [tableView reloadData];
        };
        [view showOnView:self.view];
    }
}


#pragma mark - @s
-(NSString *)getSex
{
    if ([_data[@"sex"] integerValue] == 1) {
        return @"男";
    }else if ([_data[@"sex"] integerValue] == 2){
        return @"女";
    }
    return @"未知";
}

-(NSString *)getEducation
{
    switch ([_data[@"education"] integerValue]) {
        case 0:
            return @"大专";
            break;
        case 1:
            return @"本科";
            break;
        case 2:
            return @"硕士";
            break;
        case 3:
            return @"博士";
            break;
        case 4:
            return @"其他";
            break;
        default:
            return @"未知";
            break;
    }
}

-(NSString *)getWorkingLife
{
    if ([_data[@"workingLife"] integerValue] == 0) {
        return @"应届毕业生";
    }else if ([_data[@"workingLife"] integerValue] == 11){
        return @"10年以上";
    }else{
        return [NSString stringWithFormat:@"%@年",_data[@"workingLife"]];
    }
}

-(void)onTapRightBarButton{//保存
    if (![TomamaUI isNotNullStr:_data[@"name"]]) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入姓名" timeInterval:2];
        return;
    }
    if (![TomamaUI isNotNullStr:_data[@"sex"]]) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择性别" timeInterval:2];
        return;
    }
    if (![TomamaUI isNotNullStr:_data[@"city"]]) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入所在城市" timeInterval:2];
        return;
    }
    if ([NSString stringWithFormat:@"%@",_data[@"contactTelephone"]].length != 11) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入正确的联系电话" timeInterval:2];
        return;
    }
    if (![TomamaUI isNotNullStr:_data[@"contactMailbox"]]) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入邮箱" timeInterval:2];
        return;
    }
    if (![TomamaUI isNotNullStr:_data[@"introduce"]]) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入简介" timeInterval:2];
        return;
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[Storage getToken] forKey:@"id"];
    [dic setObject:_data[@"name"] forKey:@"name"];
    [dic setObject:_data[@"sex"] forKey:@"sex"];
    if ([_data[@"education"] integerValue] == 0) {
        [_data setObject:@"0" forKey:@"education"];
    }
    if ([_data[@"workingLife"] integerValue] == 0) {
        [_data setObject:@"0" forKey:@"workingLife"];
    }
    [dic setObject:_data[@"education"] forKey:@"education"];
    [dic setObject:_data[@"workingLife"] forKey:@"workingLife"];
    [dic setObject:_data[@"city"] forKey:@"city"];
    [dic setObject:_data[@"contactTelephone"] forKey:@"contactTelephone"];
    [dic setObject:_data[@"contactMailbox"] forKey:@"contactMailbox"];
    [dic setObject:_data[@"introduce"] forKey:@"introduce"];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/updateResume" Dic:dic completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:@"保存成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    if (_blcok) {
                        _blcok(_data);
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}


@end
